package com.usian.search.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.model.behavior.pojos.ApBehaviorEntry;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.search.dto.UserSearchDto;
import com.usian.model.search.pojos.ApUserSearch;
import com.usian.model.user.pojos.ApUser;
import com.usian.search.feign.BehaviorFeign;
import com.usian.search.mapper.ApUserSearchMapper;
import com.usian.search.service.ApUserSearchService;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApUserSearchServiceImpl extends ServiceImpl<ApUserSearchMapper, ApUserSearch> implements ApUserSearchService {

    @Autowired
    private BehaviorFeign behaviorFeign;

    @Override
    public ResponseResult findUserSearch(UserSearchDto dto) {

        //参数校验
        if (dto == null || dto.getPageNum() < 0 || dto.getPageSize() < 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //获取用户信息
        ApUser user = AppThreadLocalUtils.getUser();
        if (user == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        //获取行为实体
        ApBehaviorEntry apBehaviorEntry = behaviorFeign.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
        if (apBehaviorEntry == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

//        //构建查询条件
//        QueryWrapper<ApUserSearch> queryWrapper = new QueryWrapper<>();
//        queryWrapper.lambda().eq(ApUserSearch::getStatus,1).eq(ApUserSearch::getEntryId,apBehaviorEntry.getEntryId());

        //分页查询
        Page pages = new Page(dto.getPageNum(), dto.getPageSize());
        IPage iPage = this.page(pages,Wrappers.<ApUserSearch>lambdaQuery()
                .eq(ApUserSearch::getStatus,1).eq(ApUserSearch::getEntryId,apBehaviorEntry.getId()));

        iPage.getRecords();

        return ResponseResult.okResult(iPage.getRecords());
    }

    @Override
    public ResponseResult delUserSearch(UserSearchDto dto) {

        //参数校验
        if (dto == null || dto.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //逻辑删除
        boolean update = this.update(Wrappers.<ApUserSearch>lambdaUpdate().set(ApUserSearch::getStatus, 0).eq(ApUserSearch::getId, dto.getId()));

        if (update){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }

    }

    @Override
    @Async("taskExecutor")
    public void insert(Integer entryId, String searchWords) {

        //参数校验
        if (entryId == null || searchWords == null){
            return;
        }

        ApUserSearch one = this.getOne(Wrappers.<ApUserSearch>lambdaQuery().eq(ApUserSearch::getEntryId, entryId).eq(ApUserSearch::getKeyword, searchWords));

        if (one != null){
            if (one.getStatus() == 0){
                one.setStatus(1);
                this.updateById(one);
            }else if (one.getStatus() == 1){
                return;
            }
        }else {
            ApUserSearch apUserSearch = new ApUserSearch();
            apUserSearch.setStatus(1);
            apUserSearch.setCreatedTime(new Date());
            apUserSearch.setEntryId(entryId);
            apUserSearch.setKeyword(searchWords);
            this.save(apUserSearch);
        }

    }
}
